<%@ include file="db.jsp" %>
<%@ include file="jspmkrfn.jsp" %>
<%@ page session="true" buffer="16kb" import="java.sql.*,java.util.*,java.text.*"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<% Locale locale = Locale.getDefault();
locale = new Locale("zh","CN"); 
response.setLocale(locale);%>
<%
boolean validpwd = false;
String escapeString = "\\\\'";
if (request.getParameter("submit") != null && ((String) request.getParameter("submit")).length() > 0) {

	// Setup variables
	String userid = request.getParameter("userid") + "";
	String passwd = request.getParameter("passwd") + "";
    if (!validpwd) {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = null;
			rs = stmt.executeQuery("SELECT * FROM `account_info` WHERE `ID` = '" + userid.replaceAll("'",escapeString) + "'");
			if (rs.next()) {
				if (rs.getString("PWD").toUpperCase().equals(passwd.toUpperCase())) {
					session.setAttribute("project1_status_User", rs.getString("ID"));
				 	session.setAttribute("project1_status_UserID", rs.getString("NICK"));
					validpwd = true;
				}
			}
			rs.close();
			rs = null;
			stmt.close();
			stmt = null;
			conn.close();
			conn = null;
	}
	if (validpwd) {

		// Write cookies
		if (request.getParameter("rememberme") != null && ((String)request.getParameter("rememberme")).length() > 0) {
			Cookie cookie = new Cookie("project1_userid", new String(userid));
			cookie.setMaxAge(365*24*60*60);
			response.addCookie(cookie);
		}
		session.setAttribute("project1_status", "login");
		response.sendRedirect("accountlist.jsp");
	}
}else{
	validpwd = true;
}
%>
<html>
<head>
<title>学生课表管理系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="JSPMaker v1.0.0.0" />
<link href="css/login.css" rel="stylesheet" type="text/css">
</head>
<script language="JavaScript" src="ew.js"></script>
<script language="JavaScript">
<!-- start JavaScript
function  EW_checkMyForm(EW_this) {
if  (!EW_hasValue(EW_this.userid, "TEXT" )) {
            if  (!EW_onError(EW_this, EW_this.userid, "TEXT", "请输入账号！"))
                return false;
        }
if  (!EW_hasValue(EW_this.passwd, "PASSWORD" )) {
            if  (!EW_onError(EW_this, EW_this.passwd, "PASSWORD", "请输入密码！"))
                return false;
        }
return true;
}

// end JavaScript -->
</script>
<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">
<%
Cookie cookie = null;
Cookie [] ar_cookie = request.getCookies();
String userid = "";
for (int i = 0; i < ar_cookie.length; i++){
	cookie = ar_cookie[i];
	if (cookie.getName().equals("project1_userid")){
		userid = (String) cookie.getValue();
	}
}
%>
<form action="login_account.jsp" method="post" onSubmit="return EW_checkMyForm(this);">
    <div class="bck">
        <div class="login">
		    <img src="img/customer.jpeg" alt=""/> 
			<p class="header">用户登陆</p>
			<table border="0" cellspacing="0" cellpadding="4" align="center">
				<tr>
					<td>
				    	<img src="img/id.png" alt=""/>
					</td>
				  	<td>
						<span class="jspmaker">账号:</span>
					</td>
					<td>
						<span class="jspmaker"><input class="field" type="text" name="userid" size="20"></span>
					</td>
				</tr>
				<tr>
					<td>
				    	<img src="img/pwd.png" alt=""/>
					</td>
					<td>
						<span class="jspmaker">密码:</span>
					</td>
					<td>
						<span class="jspmaker"><input class="field" type="password" name="passwd" size="20"></span>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td align="right"><span class="jspmaker"><input type="checkbox" name="rememberme" value="true">记住密码</span></td>
				</tr>
			</table>
			<span class="jspmaker"><a id="register" href="register.jsp">注册账号</a></span>
			<span class="jspmaker"><input id="denglu" type="submit" name="submit" value="登      录"></span>
        </div>
    </div>
</form>
<br>
<% if (!validpwd) { %>
<p align="center" class="alert"><span class="jspmaker" style="color: Red;">输入密码不正确！</span></p>
<% } %>
</body>
</html>
